package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyCheci;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWlCheliang;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWlYfmx;
import cn.stylefeng.guns.sys.modular.system.entity.BusConst;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyCheciMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CheciParam;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 配送车次表 服务实现类
 * </p>
 */
@Service
public class BtyCheciService extends ServiceImpl<BtyCheciMapper, BtyCheci> {
    @Resource
    private BtyCheciService checiService;
    @Resource
    private BtyWlChelianngService chelianngService;
    @Resource
    private BtyWlYfmxService yfmxService;
    @Resource
    private WeChatSendTempMsgService weChatSendTempMsgService;
    /**
     * 获取配送车次列表
     */
    public Page<Map<String, Object>> list(CheciParam checiParam) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(checiParam.getTimeLimit())) {
            String[] split = checiParam.getTimeLimit().split(" - ");
            beginTime = split[0];
            endTime = split[1] + " 23:59:59";
            checiParam.setBeginTime(beginTime);
            checiParam.setEndTime(endTime);
        }
        String riqiBegin = "";
        String riqiEnd = "";
        if (ToolUtil.isNotEmpty(checiParam.getRiqi())) {
            String[] split = checiParam.getRiqi().split(" - ");
            riqiBegin = split[0];
            riqiEnd = split[1] + " 23:59:59";
            checiParam.setRiqiBegin(riqiBegin);
            checiParam.setRiqiEnd(riqiEnd);
        }
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, checiParam);
    }
    public List<CwbbParam> dmList(){
      return baseMapper.dmList();
    }
    public  List<BtyWlCheliang>cheliangList(){
        return baseMapper.cheliangList();
    }

    public String openidList(String dm){
        return baseMapper.openidList(dm);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addItem(BtyCheci checi){
        Long cheliangId = checi.getBtyWlCheliangId();
        //获取车号相关信息
        String fzr = "";
        BtyWlCheliang cheliang = chelianngService.getById(cheliangId);
        if (ToolUtil.isNotEmpty(cheliang)) {
            checi.setSiji(cheliang.getSiji());
            checi.setDianhua(cheliang.getShoujihao());
            checi.setChehao(cheliang.getChehao());
            fzr = cheliang.getFzr();
        }
        checi.setCreateUser(LoginContextHolder.getContext().getUserId());
        checi.setCreateTime(new Date());
        Boolean flag = this.checiService.save(checi);
        if (flag == true) {
            if(ToolUtil.isNotEmpty(checi.getBtyYfmxId())){
                String psxlmc;
                if(ToolUtil.isNotEmpty(checi.getBeizhu())){
                    psxlmc = checi.getPeisongMendian()+"("+checi.getBeizhu()+")";
                }else {
                    psxlmc = checi.getPeisongMendian();
                }
                //通过运费明细id查询对应的运费信息
                BtyWlYfmx yfmx=yfmxService.getById(checi.getBtyYfmxId());
                if(ToolUtil.isNotEmpty(yfmx)){
                    yfmx.setId(UUIDUtils.getId());
                    yfmx.setFzr(fzr);
                    yfmx.setPsccId(checi.getId());
                    yfmx.setPsxlmc(psxlmc.replaceAll("\\s*", ""));
                    yfmxService.save(yfmx);
                }
            }else {
                String psxlmc;
                if(ToolUtil.isNotEmpty(checi.getBeizhu())){
                    psxlmc = checi.getPeisongMendian()+"("+checi.getBeizhu()+")";
                }else {
                    psxlmc = checi.getPeisongMendian();
                }
                //生成运费信息
                BtyWlYfmx yfmx = new BtyWlYfmx();
                yfmx.setFzr(fzr);
                yfmx.setRq(DateUtil.getNowDate());
                yfmx.setPsxlmc(psxlmc);
                yfmx.setPsccId(checi.getId());
                yfmx.setGz("200");
                yfmx.setBx("30");
                yfmx.setZj("87");
                yfmx.setCreateUser(LoginContextHolder.getContext().getUserId());
                yfmx.setCreateTime(new Date());
                yfmxService.save(yfmx);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateItem(BtyCheci checi){
        Long cheliangId = checi.getBtyWlCheliangId();
        //获取车号相关信息
        String fzr = "";
        BtyWlCheliang cheliang = chelianngService.getById(cheliangId);
        if (ToolUtil.isNotEmpty(cheliang)) {
            checi.setSiji(cheliang.getSiji());
            checi.setDianhua(cheliang.getShoujihao());
            checi.setChehao(cheliang.getChehao());
            fzr = cheliang.getFzr();
        }
        Boolean flag=   this.checiService.updateById(checi);
        if (flag == true) {
            BtyWlYfmx yfmx=yfmxService.getOne(new QueryWrapper<BtyWlYfmx>().eq("pscc_id",checi.getId()));
            String psxlmc;
            if(ToolUtil.isNotEmpty(checi.getBeizhu())){
                psxlmc = checi.getPeisongMendian()+"("+checi.getBeizhu()+")";
            }else {
                psxlmc = checi.getPeisongMendian();
            }
            //生成运费信息
            yfmx.setFzr(fzr);
            yfmx.setPsxlmc(psxlmc.replaceAll("\\s*", ""));
            yfmxService.updateById(yfmx);
        }
    }
    public void send(String ids) {
        StringBuilder sb = new StringBuilder();
        String[] idss = ids.split(",");
        for (String id : idss) {
            BtyCheci checi = checiService.getById(id);
            String facheTime = DateUtil.getDate(checi.getFacheTime());
            String[] dms = checi.getPeisongMendian().split(",");
            for (int i = 0; i < dms.length; i++) {
                //通过门店查询店长的openid
                String openid = checiService.openidList(dms[i]);
                String[] openids = new String[]{openid};
                //String[] openids=new String[]{"oM18PwW_aRtOaOH0F-YwsmMxT2sM"};
                sb.append("车辆【" + checi.getChehao() + "】").append("将于" + facheTime + "发车到门店" + dms[i]);
                String first = "车辆【" + checi.getChehao() + "】将于" + facheTime + "发车到门店" + dms[i];
                String remark = "详细信息请进入小程序，在首页的\"配送车次\"进行查询";
                String keyword1 = "配送车次通知";
                String keyword2 = "车辆【" + checi.getChehao() + "】司机【" + checi.getSiji() + "】手机号【" + checi.getDianhua() + "】将于【" + facheTime + "】发车到门店【" + dms[i] + "】";
                String keyword3 = "发车时间" + facheTime;
                String keyword4 = "无";
                JSONObject tempObj = weChatSendTempMsgService.setTempParams(first, remark, keyword1, keyword2, keyword3, keyword4);
                //推送消息
                weChatSendTempMsgService.sendmsgs(openids, BusConst.MSURL, tempObj);
            }
        }
    }
}
